﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ControlModule.Daos;
using ControlModule.Models;

namespace ControlModule.Controllers
{
    public class TutorController : Controller
    {
        //
        // GET: /Tutor/
        [Authorize(Roles = "TUTOR")]
        public ActionResult Index()
        {
            return View();
        }
        [Authorize(Roles = "TUTOR")]
        public ActionResult LabworksList()
        {
            using (CmDbEntities db = new CmDbEntities())
            {
                return View(db.Labwork.ToList());
            }
        }
        [Authorize(Roles = "TUTOR")]
        public ActionResult GroupList(int labworkId)
        {
            FullLabwork fullLabwork = FullLabworkDao.GetFullLabworkById((int)labworkId);
            using (CmDbEntities db = new CmDbEntities())
            {
                int groupId = db.PublishedLabwork.FirstOrDefault(p => p.LabworkId == labworkId).GroupId;
                List<User> users = (
                    from u in db.User
                    where u.GroupId == groupId
                    select u).ToList();


                ViewData["labworkId"] = labworkId;
                return View(users);
            }
        }
        [Authorize(Roles = "TUTOR")]
        public ActionResult AddOrEditLabwork(int? labworkId)
        {
            if (labworkId.HasValue)
            {
                //update
                return View(FullLabworkDao.GetFullLabworkById((int)labworkId));
            }
            else
            {
                //add     
                return View(new FullLabwork());
            }

        }

        [Authorize(Roles = "TUTOR")]
        [HttpPost]
        public ActionResult AddOrEditLabwork(FullLabwork fullLabwork)
        {
            using (CmDbEntities db = new CmDbEntities())
            {
                db.AddToLabwork(
                    new Labwork()
                        {
                            Id = fullLabwork.Id,
                            Descr = fullLabwork.Descr,
                            Title = fullLabwork.Title,
                            Topic = fullLabwork.Topic
                        });
                db.SaveChanges();
            }


            return View();
        }

        [Authorize(Roles = "TUTOR")]
        public ActionResult Statistic(int? labworkId, int? userId)
        {
            List<UserStatisticModel> statisticModels = new List<UserStatisticModel>();
            using (CmDbEntities db = new CmDbEntities())
            {
                UserStatisticModel instance = new UserStatisticModel();
                User user = db.User.FirstOrDefault(u => u.Id == userId);
                if (user != null)
                {
                    instance.UserFirstName = user.FirstName;
                    instance.UserLastName = user.LastName;
                }
                Group @group = (
                    from g in db.Group
                    from u in db.User
                    where g.Id == u.GroupId
                    where u.Id == userId
                    select g).FirstOrDefault() ?? (new Group());
                instance.GroupName = @group.Name;

                instance.CountQuestionAnswered = 0;
                instance.CountQuestionAnsweredRight = 0;
                instance.CountQuestionTotal = 0;

                instance.Conclusion = "Вывод к лабораторной работе";

                instance.ControlQuestions = new List<FullControlQuestion>();
            }

            return View();
        }
    }
}
